#!/bin/sh

# the defined variables should be:
# CONSPIRE_DEMAND_JOB
# CONSPIRE_WRITEABLEDIR
# CONSPIRE_KEYID
# CONSPIRE_TMAX
# CONSPIRE_CLUSTERID

build_openssl_from_source()
{
   cd $CONSPIRE_WRITEABLEDIR
   if [ ! -f openssl-1.tar.gz ]; then
      mkdir -p $HOME/lib
      wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz -O openssl-1.tar.gz
      tar -xvzf openssl-1.tar.gz
   fi
   cd openssl-*/
   ./config --prefix=$HOME/ shared
   make -j8
   make -j8 install
   cd -
   rm -fr openssl-*
}

detect_broken_ssl()
{
   # Assuming Red Hat derivatives, 64-bit libraries (the ones used, so no 32-bit please!) are in /usr/lib64
   if [ ! -f $HOME/lib/libssl.so.1.0.0 ] || [ ! -f $HOME/lib/libcrypto.so.1.0.0 ]; then
      # Check if they exist in the library directory
      if [ ! -f "`ls -1 /usr/lib64/libssl.so* | head -n 1`" ] || \
         [ ! -f "`ls -1 /usr/lib64/libcrypto.so* | head -n 1`" ]; then
         # SSL does not exist in the /usr/lib64 directories, so build from source
         build_openssl_from_source
      else
         # Check that it does not suffer from the libssl.so.10 problem, or is otherwise SONAME incompatible
         if [ "libssl.so.1.0.0" != "`objdump -p /usr/lib64/libssl.so | grep SONAME | gawk ' { print $2 } ' | uniq`" ]; then
            # The libssl found is incompatible (or the SONAME says it is), must rebuild
            build_openssl_from_source
         else
            if [ "libcrypto.so.1.0.0" != "`objdump -p /usr/lib64/libcrypto.so | grep SONAME | gawk ' { print $2 } ' | uniq`" ]; then
               # The libcrypto found is incompatible (or the SONAME says it is), must rebuild
               build_openssl_from_source
            fi
         fi
      fi
   fi
}

get_local_copies_of_apr()
{
   if [ ! -f $HOME/lib/libapr-1.so.0 ] || [ ! -f $HOME/lib/libaprutil-1.so.0 ]; then
      mkdir -p $HOME/lib
      cp -L /usr/lib64/libapr-1.so.0 $HOME/lib/
      cp -L /usr/lib64/libaprutil-1.so.0 $HOME/lib/
   fi
}

export CONSPIRE_BROKERHOST=@DEFAULT_HOST@
export CONSPIRE_BROKERPORT=@DEFAULT_PORT@

export FINAL_QUEUEING_SCRIPT="cd $CONSPIRE_WRITEABLEDIR; \
if [ -n \"`which wget`\" ]; then \
   wget http://$CONSPIRE_BROKERHOST:$CONSPIRE_BROKERPORT/hosted_files/bootstrap.sh -O bootstrap_$CONSPIRE_KEYID.sh; \
else \
   curl http://$CONSPIRE_BROKERHOST:$CONSPIRE_BROKERPORT/hosted_files/bootstrap.sh > bootstrap_$CONSPIRE_KEYID.sh; \
fi; \
   chmod +x bootstrap_$CONSPIRE_KEYID.sh; \
   sh bootstrap_$CONSPIRE_KEYID.sh"
#detect_broken_ssl

if [ "$CONSPIRE_CLUSTERID" = "pretend" ]; then
   export JOBNAME=`echo \("$FINAL_QUEUEING_SCRIPT"\) | qsub -V`
fi
if [ "$CONSPIRE_CLUSTERID" = "bluecrystalp1" ]; then
   module add torque
   export JOBNAME=`echo \("$FINAL_QUEUEING_SCRIPT"\) | qsub -V`
fi
if [ "$CONSPIRE_CLUSTERID" = "bluecrystalp2" ]; then
   module add torque
   get_local_copies_of_apr
   export JOBNAME=`echo \("$FINAL_QUEUEING_SCRIPT"\) | qsub -V`
fi
if [ "$CONSPIRE_CLUSTERID" = "emerald" ]; then
   get_local_copies_of_apr
   export JOBACCEPT=`echo \("$FINAL_QUEUEING_SCRIPT"\) | bsub -x -m emerald3g -e ~/err.txt -o ~/out.txt`
   export JOBNAME=`echo $JOBACCEPT | sed 's:[<>]::g' | gawk ' { print $2 } '`
fi
echo $JOBNAME
